﻿using UnityEngine;
using UnityEditor;
using System.Text;
using System.Collections.Generic;

public class CheckBundle : EditorWindow
{
    static GameObject obj = null;
    //static AssetBundle ab = null;
    private Dictionary<string, int> ABDependCount = new Dictionary<string, int>();


    [MenuItem("DDZ工具/列出prefab引用资源")]
    static void Init()
    {
        // Get existing open window or if none, make a new one:
        CheckBundle window = (CheckBundle)EditorWindow.GetWindow(typeof(CheckBundle));
        window.Show();

    }

    void OnGUI()
    {
        obj = EditorGUI.ObjectField(new Rect(3, 3, position.width - 6, 20), "Find Dependency", obj, typeof(GameObject)) as GameObject;
        if (obj)
        {
            Object[] roots = new Object[] { obj };

            if (GUI.Button(new Rect(3, 25, position.width - 6, 20), "Check Dependencies"))
            {
                Selection.objects = EditorUtility.CollectDependencies(roots);
            }
        }
        else
            EditorGUI.LabelField(new Rect(3, 25, position.width - 6, 20), "Missing:", "Select an object first");
    }

     void FindAb()
    {
        var allBundleNames = AssetDatabase.GetAllAssetBundleNames();
        foreach (var bundle in allBundleNames)
        {
            var dependencies = AssetDatabase.GetAssetBundleDependencies(bundle, true);
            if (dependencies.Length == 0)
            {
                Debug.Log($"not depend {bundle}");
                continue;
            }
            else
            {
                if (ABDependCount.ContainsKey(bundle))
                {
                    int count = ABDependCount[bundle];
                    count += dependencies.Length;
                    ABDependCount[bundle] = count;
                }
                else
                {
                    ABDependCount.Add(bundle, 1);
                }
            }
            var dependencyString = new StringBuilder();
        
            //foreach (var dependency in dependencies)
            //{
            //    dependencyString.Append($"\"{dependency}\" ");
            //}
            //Debug.Log($"{bundle} depends on {dependencyString}");
        }

        foreach (var count in ABDependCount)
        {
            Debug.Log($"depends count {count}");
        }
    }

    void OnInspectorUpdate()
    {
        Repaint();
    }
}